# delimit ;  
set more 1 ;  
local UNIX 0;
drop _all;
capture log close;

set mem 800m;
* global folder "C:\Users\JinKiat\Dropbox\jin";
*global folder "C:\research\hrs";
global folder "d:\Dropbox";

*global folder "\\econ-san-fs\Home4\uctpkp0\SystemFolder\Windows7\Desktop";
*global folder "C:\Users\Jeremy\Dropbox";
*global folder "~/";

         local saved  "$folder\hrs\merge\";
         local trackd "$folder\hrs\tracker\";
         local wave1d "$folder\hrs\wave1\";
         local wave2d "$folder\hrs\wave2\";
         local wave3d "$folder\hrs\wave3\";
         local wave4d "$folder\hrs\wave4\";
         local wave5d "$folder\hrs\wave5\";
		 local wave6d "$folder\hrs\wave6\";
         local wave7d "$folder\hrs\wave7\";
         local wave8d "$folder\hrs\wave8\";
		 local wave9d "$folder\hrs\wave9\";
		 local wave10d "$folder\hrs\wave10\";
		 local wave11d "$folder\hrs\wave11\";
		 local wave12d "$folder\hrs\wave12\";
		 local wave13d "$folder\hrs\wave13\";
		 local wave14d "$folder\hrs\wave14\";
		 local wave15d "$folder\hrs\wave15\";
         local aheadw1 "$folder\hrs\aheadw1\";
         local aheadw2 "$folder\hrs\aheadw2\";
		 

program drop _all;
log using `saved'huswife.log, replace;

*************************************;
* a few procedures are below ********;
*************************************;
program define renamer;
	version 6.0;
rename s_insgov1 sinsgov1;
rename s_medcare1 smedcare1;
rename s_epins1 sepins1;
rename s_eprhi1 seprhi1;
rename s_inssp1 sinssp1;
rename s_inspriv1 sinspriv1;
rename s_insexch1 sinexch1;
rename s_epfr1 sepfr1;
rename s_eprfr1 seprfr1;
rename s_eprsp1 seprsp1;
end;

program define spousev;
	version 6.0;
	local i=k;
* drop if the individual is not in this wave;
drop if (male==.); 
* generate spouses health cost variables;
rename dead`i' _dead`i';
rename oop`i' _oop`i';
rename drugc`i' _drugc`i';
rename iprem`i' _iprem`i';
rename medhmo`i' _medhmo`i';
rename medcost`i' _medc`i';
rename tc`i' _tc`i';
rename unset`i' _unset`i';
* health care utililization variables;
rename hosp`i' _hosp`i';
rename nursing`i' _nurs`i';
rename drtimes`i' _dr`i';
rename outsurg`i' _out`i';
rename dentist`i' _dent`i';
rename nodrug`i' _nod`i';
* health insurance variables;
rename insgov`i' _insg`i';
rename medcare`i' _medcare`i';
rename epins`i' _epins`i';
rename eprhi`i' _eprhi`i';
rename inssp`i' _inssp`i';
rename inspriv`i' _inspr`i';
rename insexch`i' _insexch`i';
rename realage`i' _age`i';
* spousal income variables;
rename earn`i' _earn`i';
* household variables;
rename assets`i' _assets`i';
rename stock`i' _stock`i';
rename bonds`i' _bonds`i';
rename liquid`i' _liquid`i';
rename re`i' _re`i';
rename wheels`i' _wheels`i';
rename IRA`i' _IRA`i';
rename bus`i' _bus`i';
rename trust`i' _trust`i';
rename exasset`i' _exasset`i';
rename debts`i' _debts`i';
rename remort`i' _remort`i';
rename house`i' _house`i';
rename nohouse`i' _nohouse`i';
rename mort`i' _mort`i';
rename faminc`i' _faminc`i';
rename laby`i' _laby`i';
rename selfy`i' _selfy`i';
rename socy`i' _socy`i';
rename peny`i' _peny`i';
rename othy`i' _othy`i';
rename capy`i' _capy`i';
rename SSIinc`i' _SSIinc`i';
rename WELFAREinc`i' _WELFAREinc`i';
rename VAinc`i' _VAinc`i';
rename estate`i' _estate`i';
rename estateflag`i' _estateflag`i';
rename housebequeathA`i' _housebequeathA`i';
rename housebequeathB`i' _housebequeathB`i';
	end;	

program define keeper;
	version 6.0;
	local i=k;
if `i'>1
{keep HHID sub _dead`i' _oop`i' _drugc`i' _earn`i' _iprem`i' _medhmo`i' _medc`i' _tc`i' _unset`i' _hosp`i' _nurs`i' _dr`i' _out`i' _dent`i' _nod`i' _insg`i' _medcare`i' _epins`i' _eprhi`i' _inssp`i' _inspr`i' _insexch`i' _age`i' _assets`i' _stock`i' _bonds`i' _liquid`i' _re`i' _wheels`i' _IRA`i' _bus`i' _trust`i' _exasset`i' _debts`i' _remort`i' _house`i' _nohouse`i' _mort`i'
_faminc`i' _laby`i' _selfy`i' _socy`i' _peny`i' _othy`i' _capy`i' _SSIinc`i' _WELFAREinc`i' _VAinc`i' _estate`i' _estateflag`i' _housebequeathA`i' _housebequeathB`i' };
if `i'==1
{keep HHID sub _dead`i' _oop`i' _drugc`i' _earn`i' _iprem`i' _medhmo`i' _medc`i' _tc`i' _unset`i' _hosp`i' _nurs`i' _dr`i' _out`i' _dent`i' _nod`i' _insg`i' _medcare`i' _epins`i' _eprhi`i' _inssp`i' _inspr`i' _insexch`i' _age`i' _assets`i' _stock`i' _bonds`i' _liquid`i'
_re`i' _wheels`i' _IRA`i' _bus`i' _trust`i' _exasset`i' _debts`i' _remort`i' _house`i' _nohouse`i' _mort`i'
_faminc`i' _laby`i' _selfy`i' _socy`i' _peny`i' _othy`i' _capy`i' _SSIinc`i' _WELFAREinc`i' _VAinc`i' s_*  _estate`i' _estateflag`i' _housebequeathA`i' _housebequeathB`i'};
	end;

program define replacesp;
	version 6.0;
sum _insg1 s_insgov1;
replace _insg1=s_insgov1 if _insg1==.;
sum _insg1 s_insgov1;
replace _medcare1=smedcare1 if _medcare1==.;
replace _epins1=sepins1 if _epins1==.;
replace _eprhi1=seprhi1 if _eprhi1==.;
replace _inssp1=sinssp1 if _inssp1==.;
replace _inspr1=sinspriv1 if _inspr1==.;
*replace _epfr1=s_epfr1 if epfr1==.;
*replace _eprfr1=s_eprfr1 if eprfr1==.;
*replace _eprsp1=s_eprsp1 if eprsp1==.;
replace insgov1=s_insgov1 if insgov1==.;
replace medcare1=s_medcare1 if medcare1==.;
replace epins1=s_epins1 if epins1==.;
replace eprhi1=s_eprhi1 if eprhi1==.;
replace inssp1=s_inssp1 if inssp1==.;
replace inspriv1=s_inspriv1 if inspriv1==.;
replace insexch1=s_insexch1 if insexch1==.;
replace epfr1=s_epfr1 if epfr1==.;
replace eprfr1=s_eprfr1 if eprfr1==.;
replace eprsp1=s_eprsp1 if eprsp1==.;
	end;

program define replacer;
	version 6.0;
	local i=k;
* leave "_dead" as missing if it is missing ;
replace _oop`i'=0 if merge`i'==2 & married`i'==0 & _oop`i'==.;
replace _drugc`i'=0 if merge`i'==2 & married`i'==0 & _drugc`i'==.;
replace _iprem`i'=0 if merge`i'==2 & married`i'==0 & _iprem`i'==.;
replace _medhmo`i'=0 if merge`i'==2 & married`i'==0 & _medhmo`i'==.;
replace _medc`i'=0 if merge`i'==2 & married`i'==0 & _medc`i'==.;
replace _earn`i'=0 if merge`i'==2 & married`i'==0 & _earn`i'==.;
replace _tc`i'=0 if merge`i'==2 & married`i'==0 & _tc`i'==.;
replace _unset`i'=0 if merge`i'==2 & married`i'==0 & _unset`i'==.;
replace _hosp`i'=0 if merge`i'==2 & married`i'==0 & _hosp`i'==.;
replace _nurs`i'=0 if merge`i'==2 & married`i'==0 & _nurs`i'==.;
replace _dr`i'=0 if merge`i'==2 & married`i'==0 & _dr`i'==.;
replace _out`i'=0 if merge`i'==2 & married`i'==0 & _out`i'==.;
replace _dent`i'=0 if merge`i'==2 & married`i'==0 & _dent`i'==.;
replace _nod`i'=0 if merge`i'==2 & married`i'==0 & _nod`i'==.;
replace _insg`i'=0 if merge`i'==2 & married`i'==0 & _insg`i'==.;
replace _medcare`i'=0 if merge`i'==2 & married`i'==0 & _medcare`i'==.;
replace _epins`i'=0 if merge`i'==2 & married`i'==0 & _epins`i'==.;
replace _eprhi`i'=0 if merge`i'==2 & married`i'==0 & _eprhi`i'==.;
replace _inssp`i'=0 if merge`i'==2 & married`i'==0 & _inssp`i'==.;
replace _inspr`i'=0 if merge`i'==2 & married`i'==0 & _inspr`i'==.;
replace _insexch`i'=0 if merge`i'==2 & married`i'==0 & _insexch`i'==.;
* replace household variables;
replace assets`i'=_assets`i' if assets`i'==. & _assets`i'~=.;
drop _assets`i';
replace stock`i'=_stock`i' if stock`i'==. & _stock`i'~=.;
drop _stock`i';
replace bonds`i'=_bonds`i' if bonds`i'==. & _bonds`i'~=.;
drop _bonds`i';
replace liquid`i'=_liquid`i' if liquid`i'==. & _liquid`i'~=.;
drop _liquid`i';
replace re`i'=_re`i' if re`i'==. & _re`i'~=.;
drop _re`i';
replace wheels`i'=_wheels`i' if wheels`i'==. & _wheels`i'~=.;
drop _wheels`i';
replace IRA`i'=_IRA`i' if IRA`i'==. & _IRA`i'~=.;
drop _IRA`i'; 
replace bus`i'=_bus`i' if bus`i'==. & _bus`i'~=.;
drop _bus`i';
replace trust`i'=_trust`i' if trust`i'==. & _trust`i'~=.;
drop _trust`i';
replace exasset`i'=_exasset`i' if exasset`i'==. & _exasset`i'~=.;
drop _exasset`i';
replace debts`i'=_debts`i' if debts`i'==. & _debts`i'~=.;
drop _debts`i';
replace remort`i'=_remort`i' if remort`i'==. & _remort`i'~=.;
drop _remort`i';
replace house`i'=_house`i' if house`i'==. & _house`i'~=.;
drop _house`i';
replace nohouse`i'=_nohouse`i' if nohouse`i'==. & _nohouse`i'~=.;
drop _nohouse`i';
replace mort`i'=_mort`i' if mort`i'==. & _mort`i'~=.;
drop _mort`i';

replace estate`i'=_estate`i' if estate`i'==. & _estate`i'~=.;
drop _estate`i'; 
replace estateflag`i'=_estateflag`i' if estateflag`i'==. & _estateflag`i'~=.;
drop _estateflag`i'; 
replace housebequeathA`i'=_housebequeathA`i' if housebequeathA`i'==. & _housebequeathA`i'~=.;
drop _housebequeathA`i'; 
replace housebequeathB`i'=_housebequeathB`i' if housebequeathB`i'==. & _housebequeathB`i'~=.;
drop _housebequeathB`i'; 



sum faminc`i' _faminc`i';
sum capy`i'  socy`i';
replace faminc`i'= _faminc`i' if faminc`i'==. & _faminc`i'~=.;

replace laby`i' = _laby`i' if laby`i'==. & _laby`i'~=.;
replace selfy`i' = _selfy`i' if selfy`i'==. & _selfy`i'~=.;
replace socy`i' = _socy`i' if socy`i'==. & _socy`i'~=.;
replace peny`i' = _peny`i' if peny`i'==. & _peny`i'~=.;
replace othy`i' = _othy`i' if othy`i'==. & _othy`i'~=.;
replace capy`i' = _capy`i' if capy`i'==. & _capy`i'~=.;
replace SSIinc`i' = _SSIinc`i' if SSIinc`i'==. & _SSIinc`i'~=.;
replace WELFAREinc`i' = _WELFAREinc`i' if WELFAREinc`i'==. & _WELFAREinc`i'~=.;
replace VAinc`i' = _VAinc`i' if VAinc`i'==. & _VAinc`i'~=.;
sum capy`i'  socy`i';

	end;


******************************************************************************************;
********* read in data             *******************************************************;
******************************************************************************************;
use `saved'merge3;

gen male=male1;
sort HHID PN male;
drop if HHID==HHID[_n-1] & PN==PN[_n-1];

gen chk=0;
sort HHID PN;
replace chk=1 if HHID==HHID[_n-1] & male==male[_n-1];
gen omPN=1-PN;
sort HHID omPN;
replace chk=1 if HHID==HHID[_n-1] & male==male[_n-1];

sort HHID PN;
* order chk HHID PN male sub* birthyr dead*;
* exit;

gen ones=1;
sort HHID ghealth1; * this will sort missing values of ghealth to the bottom;
replace ones=ones[_n-1]+1 if HHID==HHID[_n-1];

* my current view is that I should not drop HHIDs with multiple men/women, because the multiple men women;
* are almost always new spouses after divorce/death                                                      ;
* but keep the below code around, just in case;
tab ones;
* still to do: deal with two men/women living in the same households-should I just drop them?;
* drop if HHID==HHID[_n-1] & male==male[_n-1];
* checking code below;
*egen num=count(ones), by(HHID);
*sort num;
*by num: tab ones;
*drop if num<3;
*sort ones;
*by ones: sum ghealth*;
*drop if ones>2;

* drop if same sex, HHID AND subhhid;
sort HHID male PN;
gen dropme=0;

*Checking what each value of sub HHID represents;
*keep HHID male PN sub* heal*;
*drop healch*;
*sort HHID male PN;
*order HHID male PN sub* heal*;
*foreach x in 1 2 3 4 5 6 7 8 9 10 11 {;
*tab sub`x'w`x';
*sum heal`x' if sub`x'w`x'==0;
*sum heal`x' if sub`x'w`x'==1;
*sum heal`x' if sub`x'w`x'==2;
*sum heal`x' if sub`x'w`x'==5;
*sum heal`x' if sub`x'w`x'==6;
*sum heal`x' if sub`x'w`x'==7;
*sum heal`x' if sub`x'w`x'==8;
*};
*exit;

*Trying to isolate same sex households;
*gen missing=0;
*foreach x in 1 2 3 4 5 6 7 8 9 10 11 {;
*replace missing=1 if HHID==HHID[_n-1] & male==male[_n-1] & sub`x'w`x'==sub`x'w`x'[_n-1] & (sub`x'w`x'>-1 & sub`x'w`x'<9);
*};
*egen totmiss=max(missing), by (HHID);
*drop if totmiss==0;

gen samesex=0;
foreach x in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 {;
replace samesex=1 if HHID==HHID[_n-1] & male==male[_n-1] & sub`x'w`x'==sub`x'w`x'[_n-1] & sub`x'w`x'==0;
};
egen totsex=max(samesex), by (HHID);
*drop if totsex==1;

*keep HHID male PN sub* heal*;
*drop healch*;
*sort HHID male PN;
*order HHID male PN sub1w1 sub* heal*;
*exit;

*gen keepme=0;
*gen samesex=0;
*foreach x in 1 2 3 4 5 6 7 8 9 10 11 {;
*replace keepme=1 if sub`x'w`x'==sub`x'w`x'[_n-1] & male==male[_n-1] & HHID==HHID[_n-1] & (sub`x'w`x'>-1 & sub`x'w`x'<9);
*replace samesex=1 if HHID==HHID[_n-1] & male==male[_n-1] & sub`x'w`x'==0 & sub`x'w`x'[_n-1]==0;
*};
*egen totkeep=max(keepme), by (HHID);
*egen totsex=max(samesex), by (HHID);
*drop if totkeep==0;
*drop if totsex==1;
*exit;

*foreach x in 1 2 3 4 5 6 7 8 9 10 11 {;
*recode sub`x'w`x' (. = 1) ;
*replace sub`x'w`x'=. if HHID==HHID[_n-1] & male==male[_n-1] & sub`x'w`x'==sub`x'w`x'[_n-1] & (sub`x'w`x'>-1 & sub`x'w`x'<9);
*replace sub1w1=. if sub1w1==1 & (HHID==11936 | HHID==38308 | HHID==49263 | HHID==52894 | HHID==56083);
*drop if HHID==HHID[_n-1] & male==male[_n-1] & sub11w11==sub11w11[_n-1] & (HHID==33493 & PN==11 | HHID==34710 & PN==11);
*};
*exit;

replace sub1w1=. if sub1w1==1;

replace dropme=1 if HHID==HHID[_n-1] & male==male[_n-1] & ((sub1w1==sub1w1[_n-1] & (sub1w1>-1 & sub1w1<9))
                                                          |(sub2w2==sub2w2[_n-1] & (sub2w2>-1 & sub2w2<9))
                                                          |(sub3w3==sub3w3[_n-1] & (sub3w3>-1 & sub3w3<9)) 
                                                          |(sub4w4==sub4w4[_n-1] & (sub4w4>-1 & sub4w4<9))
                                                          |(sub5w5==sub5w5[_n-1] & (sub5w5>-1 & sub5w5<9))
                                                          |(sub6w6==sub6w6[_n-1] & (sub6w6>-1 & sub6w6<9))
                                                          |(sub7w7==sub7w7[_n-1] & (sub7w7>-1 & sub7w7<9))
                                                          |(sub8w8==sub8w8[_n-1] & (sub8w8>-1 & sub8w8<9))
                                                          |(sub9w9==sub9w9[_n-1] & (sub9w9>-1 & sub9w9<9))
                                                          |(sub10w10==sub10w10[_n-1] & (sub10w10>-1 & sub10w10<9))
                                                          |(sub11w11==sub11w11[_n-1] & (sub11w11>-1 & sub11w11<9))
                                                          |(sub12w12==sub12w12[_n-1] & (sub12w12>-1 & sub12w12<9))
                                                          |(sub13w13==sub13w13[_n-1] & (sub13w13>-1 & sub13w13<9))
                                                          |(sub14w14==sub14w14[_n-1] & (sub14w14>-1 & sub14w14<9)));



egen totdrop=max(dropme), by (HHID);
*drop if totdrop==1;

sort HHID PN;
order ones HHID PN birthyr* male*;

/*
local i = 6;
while `i' <= 10 {;
        *gen heal`i'=.; 
        *gen healch`i'=.; 
        *gen ghealth`i'=.;
                
        gen laby`i'=.; 
        gen selfy`i'=.; 
        gen socy`i'=.; 
        gen capy`i'=.; 
        gen othy`i'=.;
        gen peny`i'=.;
		gen SSIinc`i'=.;
		gen WELFAREinc`i'=.;
		gen VAinc`i'=.;

local i = `i' + 1;
};
*/

local i = 8;
while `i' <= 14 {;
 
gen remort`i'=.;

local i = `i' + 1;
};




local i = 7;
while `i' <= 14 {;
gen tc`i'=.;
*gen _tc`i'=.;

        *gen medcare`i'=.; 
        *gen epins`i'=.; 
        *gen epfr`i'=.; 
        *gen eprhi`i'=.; 
        *gen eprfr`i'=.; 
        *gen inssp`i'=.;  
	  *gen insgov`i'=.;
	  *gen inspriv`i'=.;
        *gen _dead`i'=.; 
        *gen _oop`i'=.; 
        *gen _oopd`i'=.; 
        *gen _drugc`i'=.; 
        *gen _iprem`i'=.; 
        *gen _medc`i'=.; 
        *gen _earn`i'=.; 
        *gen _hosp`i'=.; 
        *gen _nurs`i'=.; 
        *gen _dr`i'=.; 
        *gen _out`i'=.; 
        *gen _dent`i'=.; 
        *gen _nod`i'=.; 
        *gen _insg`i'=.; 
        *gen _medcare`i'=.; 
        *gen _epins`i'=.; 
        *gen _eprhi`i'=.; 
        *gen _inssp`i'=.; 
        *gen _inspr`i'=.; 
        *gen _age`i'=.;   
 
*gen house`i'=.;

*gen estateflag`i'=.;
local i = `i' + 1;
};
gen medhmo1=.;
save `saved'merge3a, replace;

*exit;

*********************************************;
* make some datasets for merging, must do this for each wave ***;
*********************************************;
drop if male==0|male==.;
renamer;
sort HHID sub1w1;
save `saved'huswife11, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub2w2; 
save `saved'huswife12, replace; *this is the dataset of just women;


drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub3w3; 
save `saved'huswife13, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub4w4; 
save `saved'huswife14, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub5w5; 
save `saved'huswife15, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub6w6; 
save `saved'huswife16, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub7w7; 
save `saved'huswife17, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub8w8; 
save `saved'huswife18, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub9w9; 
save `saved'huswife19, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub10w10; 
save `saved'huswife110, replace; *this is the dataset of just women;
drop _all;


use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub11w11; 
save `saved'huswife111, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub12w12; 
save `saved'huswife112, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub13w13; 
save `saved'huswife113, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==0|male==.;
sort HHID sub14w14; 
save `saved'huswife114, replace; *this is the dataset of just women;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
renamer;
sort HHID sub1w1;
save `saved'huswife21, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub2w2;
save `saved'huswife22, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub3w3;
save `saved'huswife23, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub4w4;
save `saved'huswife24, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub5w5;
save `saved'huswife25, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub6w6;
save `saved'huswife26, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub7w7;
save `saved'huswife27, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub8w8;
save `saved'huswife28, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub9w9;
save `saved'huswife29, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub10w10;
save `saved'huswife210, replace; *this is the dataset of just men;
drop _all;


use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub11w11;
save `saved'huswife211, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub12w12;
save `saved'huswife212, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub13w13;
save `saved'huswife213, replace; *this is the dataset of just men;
drop _all;

use `saved'merge3a;
drop if male==1|male==.;
sort HHID sub14w14;
save `saved'huswife214, replace; *this is the dataset of just men;
drop _all;

*******************************************************************;
* make datasets for men first *************************************;
*******************************************************************;
************WAVE 1********************;
use `saved'merge3a; * this is a dataset of women - merge in with men;

keep if male==0 ;

gen k=1;
spousev;
gen sub=sub1w1;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

*sum _*;


rename inssame2 s_inssame2;
keeper;
*drop sub;
rename sub sub1w1;
*drop sub;

sort HHID sub1w1;
merge HHID sub1w1 using `saved'huswife11;
drop if male==0|male==.;
gen merge1=_merge;

sum s_insgov1 _insg1;
replace _insg1=s_insgov1 if _insg1==.;

replacesp;

drop _merge;
sort HHID PN; 
save `saved'huswife11, replace;
drop _all;

************WAVE 2 *******************;
use `saved'merge3a;
keep if male==0;

gen k=2;
spousev;
gen sub=sub2w2;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;


rename inssame2 s_inssame2;
keeper;
rename sub sub2w2;
sort HHID sub2w2;

merge HHID sub2w2 using `saved'huswife12;
drop if male==0|male==.;
gen merge2=_merge;
drop _merge;

sort HHID PN; 
save `saved'huswife12, replace;
drop _all;


************WAVE 3 *******************;
use `saved'merge3a;
keep if male==0;
gen k=3;
spousev;
gen sub=sub3w3;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub3w3;
sort HHID sub3w3;
merge HHID sub3w3 using `saved'huswife13;
drop if male==0|male==.;
gen merge3=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife13, replace;
drop _all;

********** WAVE 4 *********************;
use `saved'merge3a;
keep if male==0;
gen k=4;
spousev;
gen sub=sub4w4;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub4w4;
sort HHID sub4w4;
merge HHID sub4w4 using `saved'huswife14;
drop if male==0|male==.;
gen merge4=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife14, replace;
drop _all;

******* WAVE 5 *************************;
use `saved'merge3a;
keep if male==0;
gen k=5;
spousev;
gen sub=sub5w5;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub5w5;
sort HHID sub5w5;
merge HHID sub5w5 using `saved'huswife15;
drop if male==0|male==.;
gen merge5=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife15, replace;
drop _all;


******* WAVE 6 *************************;
use `saved'merge3a;
keep if male==0;
gen k=6;
spousev;
gen sub=sub6w6;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub6w6;
sort HHID sub6w6;
merge HHID sub6w6 using `saved'huswife16;
drop if male==0|male==.;
gen merge6=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife16, replace;
drop _all;

******* WAVE 7 *************************;
use `saved'merge3a;
keep if male==0;
gen k=7;
spousev;
gen sub=sub7w7;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub7w7;
sort HHID sub7w7;
merge HHID sub7w7 using `saved'huswife17;
drop if male==0|male==.;
gen merge7=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife17, replace;
drop _all;


******* WAVE 8 *************************;
use `saved'merge3a;
keep if male==0;
gen k=8;
spousev;
gen sub=sub8w8;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub8w8;
sort HHID sub8w8;
merge HHID sub8w8 using `saved'huswife18;
drop if male==0|male==.;
gen merge8=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife18, replace;
drop _all;

******* WAVE 9 *************************;
use `saved'merge3a;
keep if male==0;
gen k=9;
spousev;
gen sub=sub9w9;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub9w9;
sort HHID sub9w9;
merge HHID sub9w9 using `saved'huswife19;
drop if male==0|male==.;
gen merge9=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife19, replace;
drop _all;

******* WAVE 10 *************************;
use `saved'merge3a;
keep if male==0;
gen k=10;
spousev;
gen sub=sub10w10;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub10w10;
sort HHID sub10w10;
merge HHID sub10w10 using `saved'huswife110;
drop if male==0|male==.;
gen merge10=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife110, replace;
drop _all;






******* WAVE 11 *************************;
use `saved'merge3a;
keep if male==0;
gen k=11;
spousev;
gen sub=sub11w11;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub11w11;
sort HHID sub11w11;
merge HHID sub11w11 using `saved'huswife111;
drop if male==0|male==.;
gen merge11=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife111, replace;
drop _all;


******* WAVE 12 *************************;
use `saved'merge3a;
keep if male==0;
gen k=12;
spousev;
gen sub=sub12w12;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub12w12;
sort HHID sub12w12;
merge HHID sub12w12 using `saved'huswife112;
drop if male==0|male==.;
gen merge12=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife112, replace;
drop _all;


******* WAVE 13 *************************;
use `saved'merge3a;
keep if male==0;
gen k=13;
spousev;
gen sub=sub13w13;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub13w13;
sort HHID sub13w13;
merge HHID sub13w13 using `saved'huswife113;
drop if male==0|male==.;
gen merge13=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife113, replace;
drop _all;



******* WAVE 14 *************************;
use `saved'merge3a;
keep if male==0;
gen k=14;
spousev;
gen sub=sub14w14;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub14w14;
sort HHID sub14w14;
merge HHID sub14w14 using `saved'huswife114;
drop if male==0|male==.;
gen merge14=_merge;
drop _merge;

sort HHID PN;
save `saved'huswife114, replace;
drop _all;





* now merge the datasets for men together;
use  `saved'huswife11;
merge HHID PN using `saved'huswife12;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife13;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife14;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife15;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife16;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife17;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife18;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife19;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife110;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife111;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife112;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife113;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife114;
drop _merge;

*_merge==1 -> unmatched women;
*_merge==2 -> unmatched men;
*_merge==3 -> matched couple; 
sort merge1;by merge1: summ married1;
sort merge2;by merge2: summ married2;
sort merge3;by merge3: summ married3;
sort merge4;by merge4: summ married4;
sort merge5;by merge5: summ married5;
sort merge6;by merge6: summ married6;
sort merge7;by merge7: summ married7;
sort merge8;by merge8: summ married8;
sort merge9;by merge9: summ married9;
sort merge10;by merge10: summ married10;
sort merge11;by merge11: summ married11;
sort merge12;by merge12: summ married12;
sort merge13;by merge13: summ married13;
sort merge14;by merge14: summ married14;

sort HHID PN;
save `saved'huswife1, replace;
drop _all;



**************************************************************************;
* now make a dataset for the women ***************************************;
**************************************************************************;
********** WAVE 1 **********************************;
use `saved'merge3a; * this is a dataset of men - merge in with women;
keep if male==1;
gen k=1;
spousev;
gen sub=sub1w1;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub1w1;
*drop sub;
sort HHID sub1w1;
merge HHID sub1w1 using `saved'huswife21;
* _merge==1 -> unmatched men, _merge==2 -> unmatched women, _merge==3 -> matched couple; 
drop if male==1|male==.;
gen merge1=_merge;

replacesp;

drop _merge;
sort HHID PN; 
save `saved'huswife21, replace;
drop _all;

************** WAVE 2 ************************************;
use `saved'merge3a;
keep if male==1;
gen k=2;
spousev;
gen sub=sub2w2;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

rename inssame2 s_inssame2;
keeper;
rename sub sub2w2;
sort HHID sub2w2;
merge HHID sub2w2 using `saved'huswife22;
* _merge==1 -> unmatched men, _merge==2 -> unmatched women, _merge==3 -> matched couple; 
drop if male==1|male==.;
gen merge2=_merge;
drop _merge;

sort HHID PN; 
save `saved'huswife22, replace;
drop _all;

********************** WAVE 3 *****************************;
use `saved'merge3a;
keep if male==1;
gen k=3;
spousev;
gen sub=sub3w3;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub3w3;
sort HHID sub3w3;
merge HHID sub3w3 using `saved'huswife23;
drop if male==1|male==.;
gen merge3=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife23, replace;
drop _all;

***************** WAVE 4 *******************************;
use `saved'merge3a;
keep if male==1;
gen k=4;
spousev;
gen sub=sub4w4;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub4w4;
sort HHID sub4w4;
merge HHID sub4w4 using `saved'huswife24;
drop if male==1|male==.;
gen merge4=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife24, replace;
drop _all;

**************** WAVE 5 **************************;
use `saved'merge3a;
keep if male==1;
gen k=5;
spousev;
gen sub=sub5w5;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub5w5;
sort HHID sub5w5;
merge HHID sub5w5 using `saved'huswife25;
drop if male==1|male==.;
gen merge5=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife25, replace;
drop _all;

**************** WAVE 6 **************************;
use `saved'merge3a;
keep if male==1;
gen k=6;
spousev;
gen sub=sub6w6;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub6w6;
sort HHID sub6w6;
merge HHID sub6w6 using `saved'huswife26;
drop if male==1|male==.;
gen merge6=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife26, replace;
drop _all;

**************** WAVE 7 **************************;
use `saved'merge3a;
keep if male==1;
gen k=7;
spousev;
gen sub=sub7w7;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub7w7;
sort HHID sub7w7;
merge HHID sub7w7 using `saved'huswife27;
drop if male==1|male==.;
gen merge7=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife27, replace;
drop _all;


**************** WAVE 8 **************************;
use `saved'merge3a;
keep if male==1;
gen k=8;
spousev;
gen sub=sub8w8;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub8w8;
sort HHID sub8w8;
merge HHID sub8w8 using `saved'huswife28;
drop if male==1|male==.;
gen merge8=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife28, replace;
drop _all;

**************** WAVE 9 **************************;
use `saved'merge3a;
keep if male==1;
gen k=9;
spousev;
gen sub=sub9w9;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub9w9;
sort HHID sub9w9;
merge HHID sub9w9 using `saved'huswife29;
drop if male==1|male==.;
gen merge9=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife29, replace;
drop _all;

**************** WAVE 10 **************************;
use `saved'merge3a;
keep if male==1;
gen k=10;
spousev;
gen sub=sub10w10;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub10w10;
sort HHID sub10w10;
merge HHID sub10w10 using `saved'huswife210;
drop if male==1|male==.;
gen merge10=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife210, replace;
drop _all;




**************** WAVE 11 **************************;
use `saved'merge3a;
keep if male==1;
gen k=11;
spousev;
gen sub=sub11w11;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub11w11;
sort HHID sub11w11;
merge HHID sub11w11 using `saved'huswife211;
drop if male==1|male==.;
gen merge11=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife211, replace;
drop _all;


**************** WAVE 12 **************************;
use `saved'merge3a;
keep if male==1;
gen k=12;
spousev;
gen sub=sub12w12;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub12w12;
sort HHID sub12w12;
merge HHID sub12w12 using `saved'huswife212;
drop if male==1|male==.;
gen merge12=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife212, replace;
drop _all;


**************** WAVE 13 **************************;
use `saved'merge3a;
keep if male==1;
gen k=13;
spousev;
gen sub=sub13w13;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub13w13;
sort HHID sub13w13;
merge HHID sub13w13 using `saved'huswife213;
drop if male==1|male==.;
gen merge13=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife213, replace;
drop _all;


**************** WAVE 14 **************************;
use `saved'merge3a;
keep if male==1;
gen k=14;
spousev;
gen sub=sub14w14;

drop if sub==.; * multiple people of the same sex in a HHID can have sub==., if for example, a woman is married to two men, but they both exit the sample;

keeper;
rename sub sub14w14;
sort HHID sub14w14;
merge HHID sub14w14 using `saved'huswife214;
drop if male==1|male==.;
gen merge14=_merge;
drop _merge;
sort HHID PN;
save `saved'huswife214, replace;
drop _all;


*****************************************;
* now merge the datasets for men together;
use  `saved'huswife21;
merge HHID PN using `saved'huswife22;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife23;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife24;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife25;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife26;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife27;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife28;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife29;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife210;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife211;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife212;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife213;
drop _merge;
sort HHID PN;
merge HHID PN using `saved'huswife214;
drop _merge;

*_merge==1 -> unmatched men;
*_merge==2 -> unmatched women;
*_merge==3 -> matched couple; 
sort merge1;by merge1: summ married1;
sort merge2;by merge2: summ married2;
sort merge3;by merge3: summ married3;
sort merge4;by merge4: summ married4;
sort merge5;by merge5: summ married5;
sort merge6;by merge6: summ married6;
sort merge7;by merge7: summ married7; * might bomb here -- I don't think I have merge7;
sort merge8;by merge8: summ married8;
sort merge9;by merge9: summ married9;
sort merge10;by merge10: summ married10;
sort merge11;by merge11: summ married11;
sort merge12;by merge12: summ married12;
sort merge13;by merge13: summ married13;
sort merge14;by merge14: summ married14;
 
sort HHID PN;
save `saved'huswife2, replace;
append using `saved'huswife1;
save `saved'huswife1, replace;

gen k=1;
replacer;
replace k=2;
replacer;
replace k=3;
replacer;
replace k=4;
replacer;
replace k=5;
replacer;
replace k=6;
replacer;
replace k=7;
replacer;
replace k=8;
replacer;
replace k=9;
replacer;
replace k=10;
replacer;
replace k=11;
replacer;
replace k=12;
replacer;
replace k=13;
replacer;
replace k=14;
replacer;
* individuals who were not financial respondent in wave1 and had same plan in wave 2;
* does employer provide health insurance?;

replace epins2=epins1 if inssame2==1;


*insurance through own firm (as opposed to spouses firm?);

replace inssp2=inssp1 if inssame2==1; 

* is the insurance free (or reduced cost)?;

replace epfr2=epfr1 if inssame2==1;

* employer provided retiree health insurance;

replace eprhi2=eprhi1 if inssame2==1;

* is the eprhi free?;

replace eprfr2=eprfr1 if inssame2==1;

* will the spouse be covered? ;

replace eprsp2=eprsp1 if inssame2==1;


sort s_inssame2;
by s_inssame2: sum _e* _i*;

* does employer provide health insurance?;

replace _epins2=_epins1 if s_inssame2==1;


*insurance through own firm (as opposed to spouses firm?);

replace _inssp2=_inssp1 if s_inssame2==1; 

* employer provided retiree health insurance;

replace _eprhi2=_eprhi1 if s_inssame2==1;


gen long hhidpn=HHID*1000+PN;
sort hhidpn;
gen duplicate=.;
replace duplicate=1 if hhidpn==hhidpn[_n-1];
drop if duplicate==1;
drop duplicate hhidpn;
*drop rage12;
save `saved'huswife1, replace;
drop _all;
program drop _all;
log close;


